/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.service;

import cn.hutool.core.collection.CollUtil;
import com.itgrail.grail.cqrs.command.BaseAppService;
import com.itgrail.grail.ddd.annotation.app.AppService;
import com.itgrail.pilot.app.command.cmo.query.ListDeptTreeCmd;
import com.itgrail.pilot.app.command.co.DeptTreeCO;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@AppService
@Slf4j
public class DeptTreeService implements BaseAppService<ListDeptTreeCmd, List<DeptTreeCO>> {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<DeptTreeCO> execute(ListDeptTreeCmd cmd) {
        if (cmd.getId() == null) {
            cmd.setId(0);
        }
        List<DeptTreeCO> list = new ArrayList<>();
        DeptTreeCO tree = tree(cmd);
        if (OkrConstants.ROOT_DEPT.equals(cmd.getId())) {
            List<DeptTreeCO> departmentList = tree.getDeptDetailCOS();
            if (CollUtil.isNotEmpty(departmentList)) {
                list.addAll(departmentList);
            }
        } else {
            list.add(tree);
        }
        return list;
    }

    private DeptTreeCO tree(ListDeptTreeCmd cmd) {
        DeptTreeCO deptTreeCO = new DeptTreeCO();
        List<DepartmentDO> departmentList = departmentMapper.selectListByParam(null);
        if (CollUtil.isNotEmpty(departmentList)) {
            Integer parentId = cmd.getId();
            DepartmentDO parentDept = departmentList.stream().filter(Objects::nonNull).filter(data -> parentId.equals(data.getParentId())).findFirst().get();
            deptTreeCO.setId(parentId);
            deptTreeCO.setDeptId(parentDept.getId());
            deptTreeCO.setName(parentDept.getName());
            List<DeptTreeCO> childTree = new ArrayList<>();
            for (DepartmentDO departmentDO : departmentList) {
                if (null != departmentDO.getParentId() && departmentDO.getParentId().equals(parentId)) {
                    DeptTreeCO childTreeCO = new DeptTreeCO();
                    childTreeCO.setId(departmentDO.getId());
                    childTreeCO.setDeptId(departmentDO.getId());
                    childTreeCO.setName(departmentDO.getName());
                    childTreeCO.setDeptDetailCOS(setChildTree(departmentDO, departmentList));
                    childTree.add(childTreeCO);
                }
            }
            deptTreeCO.setDeptDetailCOS(childTree);
        }
        return deptTreeCO;
    }

    private List<DeptTreeCO> setChildTree(DepartmentDO parentDepartmentDO, List<DepartmentDO> deptDOS) {
        List<DeptTreeCO> childTree = new ArrayList<>();
        for (DepartmentDO departmentDO : deptDOS) {
            if (departmentDO.getParentId() != null && departmentDO.getParentId().equals(parentDepartmentDO.getId())) {
                DeptTreeCO childTreeCO = new DeptTreeCO();
                childTreeCO.setId(departmentDO.getId());
                childTreeCO.setName(departmentDO.getName());
                childTreeCO.setDeptId(departmentDO.getId());
                childTreeCO.setDeptDetailCOS(setChildTree(departmentDO, deptDOS));
                childTree.add(childTreeCO);
            }
        }
        return childTree;
    }
}
